package com.photography.controller;

import com.photography.entity.PageResult;
import com.photography.entity.Result;
import com.photography.entity.StatusCode;
import com.photography.pojo.Customer;
import com.photography.pojo.Product;
import com.photography.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping
    public Result findAllByPage(Product product,
                          @RequestParam(defaultValue = "1",required = true) Integer page,
                          @RequestParam(defaultValue = "50",required = true) Integer size){
        PageResult<Product> pageResult = productService.findAll(product,page,size);
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Product> products= productService.findAllList();
        return new Result(true, StatusCode.OK, "查询成功", products);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable  String id){
        Product product = productService.findById(id);
        return new Result(true,StatusCode.OK,"查询成功",product);
    }

    @PostMapping
    public Result addProduct(@RequestBody Product product){
        productService.addProduct(product);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @DeleteMapping
    public Result updateStatusProduct(@RequestParam String id){
        productService.updateStatusProduct(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @RequestMapping("/update")
    public Result updateProductStatus(Product product){
        product.setUpdated(new Date());
        productService.updateProduct(product);
        return new Result(true,StatusCode.OK,"更新成功");
    }
    @PutMapping
    public Result updateProduct(@RequestBody Product product){
        product.setUpdated(new Date());
        productService.updateProduct(product);
        return new Result(true,StatusCode.OK,"更新成功");
    }
}
